package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.xthings.dto.BasePowermeterVo;
import com.workcell.workdata.xthings.entity.BasePowermeter;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.BasePowermeterService;
import com.workcell.workdata.xthings.service.ManufacturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功率计型号
 *
 * @author 廖博文
 * @date 2022-11-28 10:16:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basepowermeter" )
@Api(value = "basepowermeter", tags = "功率计型号管理")
public class BasePowermeterController {
    private final  BasePowermeterService basePowermeterService;
    private final ManufacturerService manufacturerService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basePowermeter 功率计型号
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getBasePowermeterPage(Page page, BasePowermeter basePowermeter) {
        return R.ok(basePowermeterService.page(page, Wrappers.query(basePowermeter)));
    }

	/**
	 * 模糊分页查询
	 * @param basePowermeterVo
	 * @return
	 */
	@ApiOperation(value = "模拟分页查询", notes = "模拟分页查询")
	@GetMapping("/vaguePage" )
	public R getBasePowermeterVaguePage(BasePowermeterVo basePowermeterVo) {
		return R.ok(basePowermeterService.getBasePowermeterVaguePage(basePowermeterVo));
	}

    /**
     * 通过id查询功率计型号
     * @param pmId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{pmId}" )
    public R getById(@PathVariable("pmId" ) String pmId) {
        return R.ok(basePowermeterService.getById(pmId));
    }

    /**
     * 新增功率计型号
     * @param basePowermeter 功率计型号
     * @return R
     */
    @ApiOperation(value = "新增功率计型号", notes = "新增功率计型号")
    @PostMapping
    public R save(@RequestBody BasePowermeter basePowermeter) {
        Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(basePowermeter.getDeviceManufacturer());
        Assert.notNull(manufacturer, "厂商查询为空");
        basePowermeter.setManufacturerShortName(manufacturer.getManufacturerShortName());
        basePowermeter.setManufacturerFullName(manufacturer.getManufacturerFullName());
        basePowermeter.setManufacturer(manufacturer.getManufacturerFullName());
        basePowermeter.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(basePowermeterService.save(basePowermeter));
    }

    /**
     * 修改功率计型号
     * @param basePowermeter 功率计型号
     * @return R
     */
    @ApiOperation(value = "修改功率计型号", notes = "修改功率计型号")
    @PutMapping
    public R updateBasePowermeter(@RequestBody BasePowermeter basePowermeter) {
		Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(basePowermeter.getDeviceManufacturer());
		Assert.notNull(manufacturer, "厂商查询为空");
		basePowermeter.setManufacturerShortName(manufacturer.getManufacturerShortName());
		basePowermeter.setManufacturerFullName(manufacturer.getManufacturerFullName());
		basePowermeter.setManufacturer(manufacturer.getManufacturerFullName());
		basePowermeter.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(basePowermeterService.updateBasePowermeter(basePowermeter));
    }

	/**
	 * 修改功率计型号
	 * @param basePowermeter 功率计型号
	 * @return R
	 */
	@ApiOperation(value = "修改功率计型号", notes = "修改功率计型号")
	@PutMapping("/updateStateById")
	public R updateStateById(@RequestBody BasePowermeter basePowermeter) {
		return R.ok(basePowermeterService.updateStateById(basePowermeter));
	}

    /**
     * 通过id删除功率计型号
     * @param pmId id
     * @return R
     */
    @ApiOperation(value = "通过id删除功率计型号", notes = "通过id删除功率计型号")
    @DeleteMapping("/{pmId}" )
    public R removeById(@PathVariable String pmId) {
        return R.ok(basePowermeterService.delBasePowermeterById(pmId));
    }


    /**
     * 导出excel 表格
     * @param basePowermeter 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<BasePowermeter> export(BasePowermeter basePowermeter) {
        return basePowermeterService.list(Wrappers.query(basePowermeter));
    }
}
